package com.example.k12.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.SurfaceView;

public class CircularSurfaceView extends SurfaceView {
    private Path circularPath;
    private Paint cutoutPaint;
    private boolean isCircular = true;
    private int overlayColor = Color.WHITE;

    public CircularSurfaceView(Context context) {
        super(context);
        init();
    }

    public CircularSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircularSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        circularPath = new Path();

        cutoutPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        cutoutPaint.setColor(Color.TRANSPARENT);
        cutoutPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        cutoutPaint.setStyle(Paint.Style.FILL);

        setZOrderOnTop(false); // 保持正常层级
        setWillNotDraw(false); // 确保调用onDraw方法
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        updateCircularPath(w, h);
    }

    private void updateCircularPath(int width, int height) {
        if (width <= 0 || height <= 0) return;

        circularPath.reset();
        float radius = Math.min(width, height) / 2f;
        float centerX = width / 2f;
        float centerY = height / 2f;
        circularPath.addCircle(centerX, centerY, radius, Path.Direction.CW);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 应用圆形裁剪
        if (isCircular && getWidth() > 0 && getHeight() > 0) {
            // 方式1: 使用遮罩
            drawCircularOverlay(canvas);
        }
    }

    public void drawCircularOverlay(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();

        if (width <= 0 || height <= 0) return;

        // 保存画布
        int saveCount = canvas.saveLayer(0, 0, width, height, null, Canvas.ALL_SAVE_FLAG);

        // 先让父类绘制（摄像头预览）
        // super.onDraw(canvas); // 这个已经在方法开始调用了

        // 绘制一个白色遮罩
        Paint overlayPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        overlayPaint.setColor(overlayColor);
        overlayPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(0, 0, width, height, overlayPaint);

        // 清除圆形区域，显示摄像头预览
        float radius = Math.min(width, height) / 2f;
        float centerX = width / 2f;
        float centerY = height / 2f;
        canvas.drawCircle(centerX, centerY, radius, cutoutPaint);

        // 恢复画布
        canvas.restoreToCount(saveCount);
    }

    /**
     * 更简单的方式：只显示圆形区域，其他区域变暗
     */
    private void drawSimpleCircularMask(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();

        if (width <= 0 || height <= 0) return;

        float radius = Math.min(width, height) / 2f;
        float centerX = width / 2f;
        float centerY = height / 2f;

        // 绘制暗色背景
        Paint dimPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        dimPaint.setColor(Color.parseColor("#80000000")); // 半透明黑色
        canvas.drawRect(0, 0, width, height, dimPaint);

        // 使用CLEAR模式清除圆形区域
        canvas.drawCircle(centerX, centerY, radius, cutoutPaint);
    }

    /**
     * 设置是否启用圆形显示
     * @param circular true为圆形，false为正常矩形
     */
    public void setCircular(boolean circular) {
        this.isCircular = circular;
        invalidate();
    }

    /**
     * 设置遮罩颜色
     * @param overlayColor 遮罩颜色
     */
    public void setOverlayColor(int overlayColor) {
        this.overlayColor = overlayColor;
        invalidate();
    }
}




